// pages/home/zhuanpan/index.js
const app = getApp();
let filter = require('../../../utils/intercept.js')
const http = require('../../../utils/http.js')
Page(filter.identityFilter({

  /**
   * 页面的初始数据
   */
  data: {
    mycolors:['#fff','#FFE9AA','#007AFF','#FF4400'],//:style="{backgroundImage: 'url('+imgUrl+')'}"
    xx:{},
    codeBool:false,
    bool:true,
    num:1,
    textColor: '#333333',
    codeNum:'',
    bgImg:'https://dss0.bdstatic.com/6Ox1bjeh1BF3odCf/it/u=1299366232,2093207817&fm=218&app=92&f=PNG?w=121&h=75&s=44B4A87254BE598C6C41EEFB03009037',
    imgUrl:'',
    imgUrl1:'https://dss0.bdstatic.com/6Ox1bjeh1BF3odCf/it/u=1299366232,2093207817&fm=218&app=92&f=PNG?w=121&h=75&s=44B4A87254BE598C6C41EEFB03009037',
    animationData:{},
    list1:['二俩小面','100积分','三俩小面','10积分','餐巾纸一包','德芙巧克力','景区门票一张','20积分','50积分','牛肉面二俩','牛肉面三俩']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.name){
      this.setData({
        name:options.name
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    http.get('/api/index/getGameInfo').then(res => {//明细接口
      if (res.code == 200) {
        if(res.data.status){
          this.setData({
            gameList:res.data,
            codeBool:res.data.codeStatus,
            list1:res.data.nameList
          })
          if(res.data.bgImg){//背景图
            this.setData({
              bgImg:res.data.bgImg
            })
          }
          if(res.data.buttonImg){//按钮图
            this.setData({
              imgUrl1:res.data.buttonImg
            })
          }
          if(res.data.borderImg){//边框图
            this.setData({
              imgUrl:res.data.borderImg
            })
          }
          // if(res.data.colors){//转盘颜色
          //   this.setData({
          //     mycolors:res.data.colors.split('|')
          //   })
          // }
          this.zhuanpan()
        }else{
          wx.showModal({
            title: '提示',
            content: '活动未开始',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                  console.log('用户点击确定')
                  
              }
          }
        })
        }
      }
    })
    
    
  },
  zhuanpan(){
    let windowPx,fx;
    wx.getSystemInfo({
      success:res=>{
        console.log(res.windowWidth)
        windowPx = res.windowWidth
        fx = windowPx/750
      }
    })
    let ctx = wx.createCanvasContext('mycanvas')
    let baseAngle = Math.PI*2/(this.data.list1.length)
    ctx.strokeStyle = '#FFBE04'
    let family = "-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', STHeiti, 'Microsoft Yahei', Tahoma, Simsun, sans-serif"
    ctx.font = `10px ${family}`
    for(let i=0;i<this.data.list1.length;i++){
      let angle = i*baseAngle
      ctx.save()
      ctx.beginPath()
      ctx.arc(324*fx, 324*fx, 324*fx, 360 / this.data.list1.length * Math.PI / 180 * i, 360 / this.data.list1.length * Math.PI / 180 * (i + 1), false)//this.canvasWidth / 2, this.canvasHeight / 2, this.canvasWidth / 2 - this.outsideWidth, 360 / this.prizeNum * Math.PI / 180 * i, 360 / this.prizeNum * Math.PI / 180 * (i + 1)
      ctx.arc(324*fx, 324*fx, 20, angle + baseAngle, angle, false)
      ctx.stroke()
      ctx.fillStyle = this.data.mycolors[i % this.data.mycolors.length]
      ctx.fill()
      // ctx.measureText()
      ctx.fill();
      ctx.closePath();
      
    }
    
    for(let i=0;i<this.data.list1.length;i++){
      
      let angle = i*baseAngle
      ctx.save();
      ctx.beginPath();
      ctx.fillStyle = "#000000";
      ctx.setTextAlign("center");//使文字垂直居中显示
      ctx.setTextBaseline("middle");//使文字水平居中显示
      ctx.translate(324*fx, 324*fx);
      ctx.rotate(360 / this.data.list1.length * Math.PI / 180 * (i + 0.51));//360/this.list1.length*i/1.85
      ctx.fillText(this.data.list1[i], 100, 0);
      ctx.restore();
    }
    ctx.draw()
  },
  running() {
    //console.log(this.data.list1)
    // this.animation.rotate(45).step({duration:1000})
    // // 调用实例的方法来描述动画,translateX定义动画类型为x轴偏移, 500为偏移长度, 单位px
    // // 调用 step() 来表示一组动画完成(当前参数动画时间1s)
    // // step 可以配置参数用于指定当前组动画的配置。具体参数请看文档
    // this.animationData = this.animation.export()
    // // export方法导出动画数据
    var that=this
    let codeNum=''
    if(this.data.codeNum){
      codeNum=this.data.codeNum
    }
    if(this.data.bool){
      this.setData({
        bool:false
      })
      http.get('/api/index/beginLuckDraw', {gameCode:codeNum}).then(res => {
        var mydata = res.data
          if (res.code == 200) {
          
            let animation = wx.createAnimation({
              timingFunction: 'ease'
            })
            this.animation = animation
            console.log(this.data.list1)
            console.log(this.data.list1.length)
            var asd=(360*10)-(360/this.data.list1.length)*res.data.index-360/this.data.list1.length
            //animation.rotate(45).step({duration: 500,timingFunction:'ease-in-out'})
            animation.rotate(asd).step({duration:2000})
            //animation.rotate((360*10)-(360/this.data.list1.length)*res.data.index-90-360/this.data.list1.length/2).step({duration:2000})
            console.log('asdasdasd')
            this.setData({
              animationData : animation.export()
            })
            setTimeout(function () {
              wx.showModal({
                title: '恭喜您',
                content: '抽中奖品：'+that.data.list1[res.data.index],
                showCancel:false,
                success: function (res) {
                    if (res.confirm) {
                        console.log('用户点击确定');
                        animation.rotate(0).step({duration:0})
                        that.setData({
                          animationData : animation.export(),
                          bool:true
                        })
                        if(mydata.productType == 1 || mydata.productType == 2){
                          console.log('asd')
                        }
                        if(that.data.gameList.codeStatus){
                          that.setData({
                            codeBool:true
                          })
                        }
                    }
                }
              });
            }, 2000)
          }
      })
    }
  },
  bindKeyInput: function(e){
    this.setData({
      codeNum:e.detail.value
    })
  },
  codeBtn:function(){

    http.get('/api/index/checkGameCode', {code:this.data.codeNum,gameId:this.data.gameList.id}).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title:'正确的抽奖码'
        })
        this.setData({
          codeBool:false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))